package com.apimanage.api.service.impl;

import com.apimanage.api.entity.Api;
import com.apimanage.api.entity.ApiVersion;
import com.apimanage.api.mapper.ApiMapper;
import com.apimanage.api.mapper.ApiVersionMapper;
import com.apimanage.api.service.ApiService;
import com.apimanage.common.exception.ApiException;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * API服务实现类
 *
 * @author API管理平台
 */
@DubboService
public class ApiServiceImpl implements ApiService {

    @Autowired
    private ApiMapper apiMapper;

    @Autowired
    private ApiVersionMapper apiVersionMapper;

    @Override
    @Transactional
    public Api createApi(Api api) {
        api.setCreateTime(new Date());
        api.setUpdateTime(new Date());
        api.setStatus(0); // 初始状态为草稿
        apiMapper.insert(api);
        return api;
    }

    @Override
    @Transactional
    public Api updateApi(Api api) {
        Api existingApi = apiMapper.selectById(api.getId());
        if (existingApi == null) {
            throw new ApiException("API不存在");
        }

        api.setUpdateTime(new Date());
        apiMapper.updateById(api);
        return api;
    }

    @Override
    @Transactional
    public boolean deleteApi(Long id) {
        Api api = apiMapper.selectById(id);
        if (api == null) {
            throw new ApiException("API不存在");
        }

        // 先删除版本信息
        List<ApiVersion> versions = apiVersionMapper.selectByApiId(id);
        for (ApiVersion version : versions) {
            apiVersionMapper.deleteById(version.getId());
        }

        // 再删除API信息
        apiMapper.deleteById(id);
        return true;
    }

    @Override
    public Api getApiById(Long id) {
        Api api = apiMapper.selectById(id);
        if (api == null) {
            throw new ApiException("API不存在");
        }
        return api;
    }

    @Override
    public List<Api> getApiPage(Map<String, Object> params) {
        // 计算分页参数
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        params.put("offset", (pageNum - 1) * pageSize);
        
        return apiMapper.selectApiPage(params);
    }

    @Override
    @Transactional
    public boolean publishApi(Long id) {
        Api api = apiMapper.selectById(id);
        if (api == null) {
            throw new ApiException("API不存在");
        }

        // 检查是否有版本
        List<ApiVersion> versions = apiVersionMapper.selectByApiId(id);
        if (versions.isEmpty()) {
            throw new ApiException("请先创建API版本");
        }

        // 设置状态为已发布
        api.setStatus(1);
        api.setUpdateTime(new Date());
        apiMapper.updateById(api);
        return true;
    }

    @Override
    @Transactional
    public boolean offlineApi(Long id) {
        Api api = apiMapper.selectById(id);
        if (api == null) {
            throw new ApiException("API不存在");
        }

        // 设置状态为已下线
        api.setStatus(2);
        api.setUpdateTime(new Date());
        apiMapper.updateById(api);
        return true;
    }

    @Override
    @Transactional
    public ApiVersion createApiVersion(ApiVersion apiVersion) {
        // 检查API是否存在
        Api api = apiMapper.selectById(apiVersion.getApiId());
        if (api == null) {
            throw new ApiException("API不存在");
        }

        apiVersion.setCreateTime(new Date());
        apiVersion.setUpdateTime(new Date());
        apiVersionMapper.insert(apiVersion);
        return apiVersion;
    }

    @Override
    public List<ApiVersion> getApiVersions(Long apiId) {
        return apiVersionMapper.selectByApiId(apiId);
    }

    @Override
    public ApiVersion getCurrentVersion(Long apiId) {
        ApiVersion version = apiVersionMapper.selectCurrentVersion(apiId);
        if (version == null) {
            // 如果没有设置当前版本，返回最新创建的版本
            List<ApiVersion> versions = apiVersionMapper.selectByApiId(apiId);
            if (!versions.isEmpty()) {
                return versions.get(0);
            }
        }
        return version;
    }

    @Override
    @Transactional
    public boolean setCurrentVersion(Long versionId) {
        ApiVersion version = apiVersionMapper.selectById(versionId);
        if (version == null) {
            throw new ApiException("版本不存在");
        }

        // 取消当前API的所有版本的当前版本标记
        apiVersionMapper.cancelCurrentVersion(version.getApiId());

        // 设置指定版本为当前版本
        version.setIsCurrent(1);
        version.setUpdateTime(new Date());
        apiVersionMapper.updateById(version);
        return true;
    }
}